<?php

namespace Common\Controller;

use Think\Controller;

//use OSS\OssClient;

/**
 * 前台公共控制器
 * 为防止多分组Controller名称冲突，公共Controller名称统一使用分组名称
 */
class BaseController extends Controller
{
    public $current_user = null;

    /* 空操作，用于输出404页面 */
    public function _empty()
    {
        $this->redirect('Index/index');
    }

    protected function _initialize()
    {
        require_once(BASE_PATH . 'vendor/autoload.php');
//        return true;
        $this->login();
        $this->current_user = session('user_auth');
//        $this->assign('user_auth', $this->current_user);
    }

    /* 用户登录检测 */
    protected function login()
    {
        /* 用户登录检测 */
        is_login() || $this->redirect('/');//$this->error('您还没有登录，请先登录！', U('/'));
    }

//    protected function user()
//    {
//        $user = M('sys_user');
//        $data = $user->where('state>=0')->select();
//        return $data;
//    }

    public function downfile()
    {
        $annex_id = $_GET['annex_id'];
        if (empty($annex_id)) {
            exit();
        }
        $id = C('OSS_ID');
        $key = C('OSS_KEY');
        $endpoint = C('OSS_ENDPOINT');
        $bucket = "xmwmb";

        $model = M('UserTaskAnnex');
        $result = $model->where('id=%d', $annex_id)->find();
        if (empty($result)) {
            exit();
        }
        $object = $result['annex_path'];
        $file_name = $result['annex_name'];

        try {
            $ossClient = new \OSS\OssClient($id, $key, $endpoint);
            $content = $ossClient->getObject($bucket, $object);

            Header("Content-type: application/octet-stream");
            Header("Content-Disposition: attachment; filename=" . $file_name);
            echo $content;
        } catch (OssException $e) {
            printf(__FUNCTION__ . ": FAILED\n");
            printf($e->getMessage() . "\n");
            return;
        }
    }
}
